﻿using System;
using Core.Security.CoreMembership;
using Core.Message.Entities;
using Core.ExceptionHandling;
using System.Web.Security;
using Core.Common.Session;
using vHCS.SYS.Business;
using vHCS.SYS.Entities;
using System.Web;
using System.Web.UI.WebControls;
using Core.NTier.UserInterface;
using Core.Controls.Message;

public partial class Account_Login : BaseFormNotAuthorization
{
    #region CONST
    private const string _PG_LOCAL_LOGIN = "LocalLogin.aspx?ReturnURL={0}";
    private const string _PG_DEFAULT = "~/Default.aspx";
    private const string _CK_USERNAME = "LOGIN_CK_USERNAME";
    private const string _CK_PASSWORD = "LOGIN_CK_PASSWORD_{0}";
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            lblLoginFail.Visible = false;
        }
    }
    /// <summary>
    /// Click nut Login
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        
        try
        {
            CoreMembershipProvider member = new CoreMembershipProvider();
            CoreMembershipProvider.CurrentLoginState = LoginState.LDAP;
            bool isValid=member.ValidateUser(this.txtUsername.Text.Trim().ToLower(), this.txtPassword.Text);
            if (isValid)
            {
                this.SetCookie();
                FormsAuthentication.RedirectFromLoginPage(this.txtUsername.Text.ToLower(), this.chkRemember.Checked);
            }
            else if (CoreMembershipProvider.CurrentLoginState == LoginState.LOCAL)
            {
                this.SetCookie();
                string returnURL = this.Request.QueryString["ReturnURL"];
                if (string.IsNullOrEmpty(returnURL)) returnURL = _PG_DEFAULT;
                Response.Redirect(string.Format(_PG_LOCAL_LOGIN, returnURL));
            }                            
           
        }
        catch (Exception ex)
        {
            AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
            MessageBox.Show(mess);
        }
    }

    /// <summary>
    /// Lưu thông tin mật khẩu vào cookie
    /// </summary>
    protected void SetCookie() {
        if (this.chkRemember.Checked)
        {
            HttpCookie usCookie = new HttpCookie(_CK_USERNAME);
            HttpCookie psCookie = new HttpCookie(_CK_PASSWORD);
            //int days = Core.Configs.CoreConfigManager.DaysOfPasswordExpiration;

            usCookie.Value = this.txtUsername.Text.Trim().ToLower();
            psCookie.Value = Core.Cryptography.Cryptographer.EncryptSymmetric(this.txtPassword.Text);

            //usCookie.Expires = DateTime.Now.AddDays(days);
            //psCookie.Expires = DateTime.Now.AddDays(days);

            Response.Cookies.Add(usCookie);
            Response.Cookies.Add(psCookie);
            
        }
    }

    /// <summary>
    /// Lấy thông tin mật khẩu
    /// </summary>
    protected void GetCookie() {
        try
        {           
            //Thông tin mật khẩu đã lưu
            HttpCookie usCookie = new HttpCookie(_CK_USERNAME);
            usCookie = Request.Cookies[_CK_USERNAME];
            HttpCookie psCookie = new HttpCookie(_CK_PASSWORD);
            psCookie = Request.Cookies[_CK_PASSWORD];
            if (usCookie != null)
                this.txtUsername.Text = usCookie.Value;
            if (psCookie != null && string.IsNullOrEmpty(psCookie.Value)==false)
            {
                this.txtPassword.TextMode = TextBoxMode.Password;
                string pass=Core.Cryptography.Cryptographer.DecryptSymmetric(psCookie.Value);
                this.txtPassword.Text = pass;
                this.txtPassword.Attributes.Add("value",pass);
            }
           
        }
        catch (Exception ex)
        {
            AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
            lblLoginFail.Text = (SessionManager.IsEnglish) ? mess.EN : mess.VN;
            lblLoginFail.Visible = true;
        }
    }
}